//index.js
//获取应用实例
const app = getApp();

Page({
    data: {
        avatar: '/images/4.jpg',
        bg: '/images/3.jpg',
        qcode: '/images/village.png',
        name: '芒果妖精',
        title: "给您推荐好东西",
        shopName: '远山茶书房－1楼6人茶室',
        outTime: '出行时间：20190912',
        priceType: '￥',
        price: '60',
        isShow:false
    },
    drawImage() {
        wx.showLoading({
            title: '绘制中...',
        })
        this.setData({
            isShow:true
        })
        let ctx = wx.createCanvasContext('shareCanvas', this)
        ctx.save()
        ctx.setFontSize(13)
        ctx.setFillStyle('#313131')
        ctx.fillText(this.data.name, 64, 30)
        ctx.setFontSize(11)
        ctx.setFillStyle('#878787')
        ctx.fillText(this.data.title, 64, 46)
        ctx.drawImage(this.data.bg, 0, 60, 290, 264)
        ctx.setFontSize(13)
        ctx.setFillStyle('#313131')
        ctx.fillText(this.data.shopName, 14, 355)
        ctx.setFontSize(12)
        ctx.setFillStyle('#777777')
        ctx.fillText(this.data.outTime, 14, 373)
        ctx.setFontSize(14)
        ctx.setFillStyle('#F54B1E')
        ctx.fillText(this.data.priceType, 14, 401)
        ctx.setFontSize(20)
        ctx.setFillStyle('#F54B1E')
        ctx.fillText(this.data.price, 28, 401)
        ctx.drawImage(this.data.qcode, 210, 340, 58, 58)
        ctx.arc(34, 31, 20, 0, 2 * Math.PI)
        ctx.clip()
        ctx.drawImage(this.data.avatar, 14, 11, 40, 40)
        ctx.draw()
        wx.showToast({
            title: '绘制成功',
        })
    },
    async onReady() {

    },
    handleDownload() {
        let $this = this;
        // 把图片存到本地
        wx.getSetting({
            success(res) {
                if (!res.authSetting['scope.writePhotosAlbum']) {
                    wx.authorize({
                        scope: 'scope.writePhotosAlbum',
                        success() {
                            // 用户已经同意小程序使用相册
                            $this.wxDownload()
                        },
                        fail() {
                            wx.openSetting({
                                success: (res) => {
                                }
                            })
                        }
                    })
                } else {
                    // 用户已经同意小程序使用相册
                    $this.wxDownload()
                }
            }
        })
    },

    wxDownload(){
        wx.canvasToTempFilePath({
            canvasId: 'shareCanvas',
            success(res) {
                console.log(res)
                console.log(res.tempFilePath)
                wx.saveImageToPhotosAlbum({
                    filePath:res.tempFilePath,
                    success(res){
                        console.log(res)
                    },
                    fail(e){
                        console.log(e)
                    }
                })
            },
            fail(e){
                console.log(e)
            },
            complete(){

            },
        })
    },
    async onLoad() {
    }
});
